﻿using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
using 纺织贸易管理系统;
using Sunny.UI;
using BLL;
using DevExpress.XtraEditors;
using Tools;
using 纺织贸易管理系统.选择窗体;
using 纺织贸易管理系统.设置窗体;
using Newtonsoft.Json;
using 纺织贸易管理系统.其他窗体;
using SqlSugar;
using SqlSugar.Extensions;

namespace 后整理管理系统.出库管理
{
  
    public partial class 销售客诉单 : Form
    {  private FormUseful formUseful = new FormUseful();
        private CustomerComplaints customerComplaints = new CustomerComplaints();
        private List<danjumingxitable> details = new List<danjumingxitable>();
    private string customerNum = string.Empty;
        public 销售客诉单(CustomerComplaints customerComplaints, FormUseful Useful)
        {
            InitializeComponent();
            try
            {
            CreateGrid.Create(this.Name, gridView1);
           
            }
            catch
            {
                配置列ToolStripMenuItem_Click(null, null);
            }
            this.customerComplaints = customerComplaints;
            this.formUseful = Useful;
        }
        private CustomerComplaints 单据赋值()
        {
            return new CustomerComplaints()
            {
                ID = customerComplaints.ID,
                SaleMan = cmb业务员.Text,
                CauseAnalysis = txt原因分析.Text,
                DocNum = txtdanhao.Text,
                ComplaintsDate = dtp客诉日期.Value,
                ComplaintsMoney = txt客诉金额.Text.ToDecimal(),
                ComplaintsStyle = cmbFahuogongsi.Text,
                DocDate = dtpDocDate.Value,
                HandlingScenarios = txt处理方案.Text,
                LongtermImprovementoptions = txt长期改善.Text,
                ReceiveMan = cmb接受人.Text,
                SupplierName = txtkehu.Text,
                DescriptionException = txt异常描述.Text,
                IsAudit = false,
                DocStyle = DanjuLeiXing.销售客诉单,
                SupplierNum = customerNum,
            };
        }
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridView1.CloseEditor();
            try
            {
                Connect.dbcontext.BeginTran ();
                if (formUseful == FormUseful.新增)
                {
                    Connect.dbcontext.Insertable(单据赋值()).ExecuteCommand();
                    Connect.dbcontext.Insertable (details.Where(x => !string.IsNullOrEmpty(x.pingming)).ToList()).ExecuteCommand ();
                }
                else
                {
                    销售客诉BLL.反审核(txtdanhao.Text);
                    Connect.dbcontext.Updateable (单据赋值()).ExecuteCommand ();
                    Connect.dbcontext.Deleteable <danjumingxitable>(x => x.danhao == txtdanhao.Text).ExecuteCommand ();
                    Connect.dbcontext.Insertable (details.Where(x => !string.IsNullOrEmpty(x.pingming)).ToList()).ExecuteCommand ();
                }
                销售客诉BLL.审核通过(txtdanhao.Text);
                Connect.dbcontext.CommitTran();
                AlterDlg.Show("保存成功");
                formUseful = FormUseful.新增;
                InitDoc(new CustomerComplaints());
            }
            catch (Exception ex)
            {
                Connect.dbcontext.RollbackTran();
                Tools.ShowMessageBox.ShowErrorBox(ex.Message);
            }
        }
        private void InitDoc(CustomerComplaints doc)
        {
            dtpDocDate.Value = doc.DocDate;
            txtdanhao.Text = formUseful == FormUseful.新增 ? BianhaoBLL.CreatDanhao(FirstLetter.销售客诉单, dtpDocDate.Value , DanjuLeiXing.销售客诉单): doc.DocNum ;
            cmbFahuogongsi.SelectedIndex = 0;
            txtkehu.Text = doc.SupplierName;
            txt异常描述.Text = doc.DescriptionException;
            txt原因分析.Text = doc.CauseAnalysis;
            txt处理方案.Text = doc.HandlingScenarios;
            txt客诉金额.Text = doc.ComplaintsMoney.ToString();
            txt长期改善.Text = doc.LongtermImprovementoptions;
            cmb业务员.Text = doc.SaleMan;
            cmb接受人.Text = doc.ReceiveMan;
            dtp客诉日期.Value = doc.ComplaintsDate;
            customerNum = doc.SupplierNum;
            if (formUseful == FormUseful.新增)
            {
                details.Clear();
            }
            else
            {
                details = Connect.dbcontext.Queryable <danjumingxitable>().Where (x => x.danhao == doc.DocNum).ToList ();
            }
            for (int i = 0; i < 30 - details.Count; i++)
            {
                details.Add(new danjumingxitable() { danhao = txtdanhao.Text,IsHanshui=IsHanshuiModel.未税 });
            }
            gridControl1.DataSource = details;
            gridControl1.RefreshDataSource();
        }

        private void 销售客诉单_Load(object sender, EventArgs e)
        {
            if (formUseful == FormUseful.新增)
            {
                InitDoc(new CustomerComplaints());
            }
            else
            {
                InitDoc(customerComplaints);
            }
            if (formUseful == FormUseful.查看)
            {
                保存ToolStripMenuItem.Enabled = false;
                打印单据ToolStripMenuItem.Enabled = false;
            }
        }

        private void 打印编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Print(PrintModel.Design);
        }
        private void Print(PrintModel useful)
        {
            if(details.Where(x=>!string.IsNullOrEmpty (x.pingming )).Count()==0)
            {
                ShowMessageBox.ShowErrorBox("没有任何客诉的布料信息");
                return;
            }
            var danju = new 打印客诉单()
            {
                业务员 = cmb业务员.Text,
                单据日期 = dtpDocDate.Value,
                原因分析 = txt原因分析.Text,
                处理方案 = txt处理方案.Text,
                客商名称 = txtkehu.Text,
                客诉单号 = txtdanhao.Text,
                客诉日期 = dtp客诉日期.Value,
                客诉类型 = cmbFahuogongsi.Text,
                客诉金额 = txt客诉金额.Text.ToDecimal(),
                异常描述 = txt异常描述.Text,
                接收人 = cmb接受人.Text,
                长期改善措施 = txt长期改善.Text,
                ReportPath= PrintPath.报表模板
            };
            danju.打印(useful, danju, details.Where(x => !string.IsNullOrEmpty(x.pingming)).ToList());
        }

        private void txtkehu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var fm = new 客户选择() { linkman = new LXR() { MC = "", ZJC = "" } };
            fm.ShowDialog();
            txtkehu.Text = fm.linkman.MC;
            customerNum = fm.linkman.BH;
        }
        private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Print(PrintModel.Privew);
        }

        private void 直接打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Print(PrintModel.Print);
        }

        private void 配置列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm = new 配置列(gridView1) { formname = this.Name, Obj = new danjumingxitable() };
            fm.ShowDialog();
        }

        private void 保存样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateGrid.SaveGridview(this.Name, gridView1);
        }

        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridView1.DeleteRow(gridView1.FocusedRowHandle);
        }

        private void 添加行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridView1.AddNewRow();
        }

        private void 复制行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(JsonConvert.SerializeObject(details[gridView1.FocusedRowHandle]));
        }

        private void 粘贴行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
          
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            details[gridView1.FocusedRowHandle].hanshuiheji = details[gridView1.FocusedRowHandle].hanshuidanjia * details[gridView1.FocusedRowHandle].chengpingmishu;
            txt客诉金额.Text = details.Sum(x => x.hanshuiheji).ToString ();
        }
    }
}
